package com.jqgrid.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jqgrid.search.Jqgrid.Groups;
import com.jqgrid.search.Jqgrid.SearchOperationUtil;
import com.jqgrid.search.mybatis.mapper.TbUserMapper;
import com.jqgrid.search.service.UserServcie;
import com.jqgrid.search.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Slf4j
public class UserServcieImpl implements UserServcie {


    @Autowired
    private TbUserMapper mapper;

    @Override
    public PageInfo<UserVo> listUser(HttpServletRequest request) {


        String querySql = null;

        int rows = Integer.parseInt(request.getParameter("rows"));
        int page = Integer.parseInt(request.getParameter("page"));


        String s_filters = request.getParameter("filters");

        if (StringUtils.isNoneEmpty(s_filters)) {

            Groups groups = new Groups();
            groups = JSON.parseObject(s_filters, Groups.class);

            log.debug("查询参数:" + JSON.toJSONString(groups));

            querySql = SearchOperationUtil.getStringForGroup(groups);

        }

        PageHelper.startPage(page, rows);
        List<UserVo> list = mapper.listUser(querySql);
        PageInfo<UserVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
